package me.raih.controller.admin.common;

import me.raih.pojo.po.Bill;
import me.raih.pojo.po.Sales;
import me.raih.pojo.po.User;
import me.raih.pojo.po.Wares;
import me.raih.pojo.vo.PageBean;
import me.raih.pojo.vo.ResultInfo;
import me.raih.service.BillService;
import me.raih.service.SalesService;
import me.raih.service.UserService;
import me.raih.service.WaresService;
import me.raih.util.CaseBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author Raih
 * @Time 2021/3/17  7:36
 *
 * 管理员订单管理
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminBillController {

    @Autowired
    BillService billService;
    @Autowired
    UserService userService;
    @Autowired
    WaresService waresService;
    @Autowired
    SalesService salesService;

    @RequestMapping(value = "/admin/bill")
    public String toBill(){

        return "admin/bill/bills";
    }


    @RequestMapping(value = "/findBillPage")
    @ResponseBody
    public ResultInfo adminFindBill(HttpServletRequest request,
                               @RequestParam("billNo")String billNo,
                               @RequestParam("currentPage")int currentPage,
                               @RequestParam("param")String param,
                               @RequestParam("pageSize")String pageSize,
                               @RequestParam("salesName")String salesName,
                               @RequestParam("username")String username,
                               @RequestParam("bigPage")int bigPage){
        ResultInfo resultInfo = new ResultInfo();
        int pageSizeInt;
        try {
            pageSizeInt = Integer.parseInt(pageSize);
        }catch (Exception e){
            pageSizeInt = 5;
        }
        PageBean<Bill> billPageBean = billService.findBillByBillNoAndPageAndUsernameAndSalesName(billNo,currentPage,bigPage,param,pageSizeInt,username,salesName);

        if(billPageBean.getList() == null || billPageBean.getList().size() == 0){
            resultInfo.setFlag(false);
            resultInfo.setMessage("数据为空");
        }else {
            resultInfo.setFlag(true);
            resultInfo.setData(billPageBean);
            //把所有的订单放到session中

            request.getSession().setAttribute("ADMIN_BILL_WARES_MESSAGE",billPageBean.getList());
        }

        return resultInfo;
    }

    @RequestMapping(value = "/bill/showUserMessage")
    public String toUserMessage(@RequestParam("username") String username,HttpServletRequest request){

        //通过用户名查找用户
        User user = userService.findUserByUsername(username);

        request.setAttribute("ADMIN_BILL_USER_MESSAGE",user);
        return "admin/bill/billUserMessage";
    }

    @RequestMapping(value = "/bill/showSalesMessage")
    public String b(@RequestParam("salesName") String salesName,Model model){


        Sales sales = salesService.findSalesBySalesName(salesName);

        model.addAttribute("ADMIN_BILL_SALES_MESSAGE",sales);

        return "admin/bill/billSalesMessage";
    }

    /**
     * 去查看商品详情
     * @param billNo 订单
     * @param request 请求
     * @return 商品详情页面
     */
    @RequestMapping(value = "/bill/showWaresMessage")
    public String toWaresMessage(@RequestParam("billNo") String billNo, HttpServletRequest request){

        Object bill_wares_message = request.getSession().getAttribute("ADMIN_BILL_WARES_MESSAGE");
        List<Bill> billList = CaseBeanUtil.castBean(bill_wares_message, Bill.class);
        //存放所有的商品信息
        List<Wares> waresList = new ArrayList<>();
        for (Bill bill : billList) {
            //如果两个订单号相同，就是我们要找的
            if(bill.getBillNo() .equals(billNo)){
                //通过该订单获取该订单下的商品信息
                Map<String, Integer> map = bill.getSellWaresMap();
                //遍历所有的 key 和 value
                for (String key :map.keySet()) {
                    //通过商品号查询商品信息
                    Wares wares = waresService.findWaresByNo(key);
                    //商品的数量信息不是库存数量，而是客户购买的数量，也就是map对应的value
                    int number = map.get(key);
                    wares.setNumber(number);
                    //设置商品总价，把进价改成总价
                    wares.setTradePrice(number*wares.getRetailPrice());
                    //积分改成总积分
                    wares.setPoints(wares.getPoints()*number);
                    //添加到集合中
                    waresList.add(wares);
                }
                //传到前台
                request.setAttribute("ADMIN_BILL_WARES_MESSAGE",waresList);
            }
        }

        return "admin/bill/billWaresMessage";
    }

}
